package com.ruoyi.web.controller.ddsystem;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DdUser;
import com.ruoyi.system.service.IDdUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户信息Controller
 * 
 * @author ambcc
 * @date 2020-02-10
 */
@Controller
@RequestMapping("/system/dduser")
public class DdUserController extends BaseController
{
    private String prefix = "system/dduser";

    @Autowired
    private IDdUserService ddUserService;

    @Autowired
    private SysPasswordService passwordService;
    @RequiresPermissions("system:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 查询客户信息列表
     */
    @RequiresPermissions("system:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DdUser ddUser)
    {
        startPage();
        List<DdUser> list = ddUserService.selectDdUserList(ddUser);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @RequiresPermissions("system:user:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DdUser ddUser)
    {
        List<DdUser> list = ddUserService.selectDdUserList(ddUser);
        ExcelUtil<DdUser> util = new ExcelUtil<DdUser>(DdUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 新增客户信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户信息
     */
    @RequiresPermissions("system:user:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DdUser ddUser)
    {

        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(ddUserService.checkLoginNameUnique(ddUser.getLoginName())))
        {
            return error("新增用户'" + ddUser.getLoginName() + "'失败，登录账号已存在");
        }
        else if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(ddUserService.checkPhoneUnique(ddUser)))
        {
            return error("新增用户'" + ddUser.getLoginName() + "'失败，手机号码已存在");
        }
        else if (UserConstants.USER_EMAIL_NOT_UNIQUE.equals(ddUserService.checkEmailUnique(ddUser)))
        {
            return error("新增用户'" + ddUser.getLoginName() + "'失败，邮箱账号已存在");
        }
        ddUser.setSalt(ShiroUtils.randomSalt());
        ddUser.setPassword(passwordService.encryptPassword(ddUser.getLoginName(), ddUser.getPassword(), ddUser.getSalt()));
        ddUser.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(ddUserService.insertDdUser(ddUser));
    }

    /**
     * 修改客户信息
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        DdUser ddUser = ddUserService.selectDdUserById(userId);
        mmap.put("ddUser", ddUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户信息
     */
    @RequiresPermissions("system:user:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DdUser ddUser)
    {
        return toAjax(ddUserService.updateDdUser(ddUser));
    }

    /**
     * 删除客户信息
     */
    @RequiresPermissions("system:user:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ddUserService.deleteDdUserByIds(ids));
    }
}
